/*
 * Jed.java
 *
 * Created on February 11, 2002, 8:43 AM
 */

package driver.ted;
import java.awt.*;
import java.awt.event.*;
import javax.swing.text.*;
import javax.swing.*;
import java.util.HashMap;
import java.util.Map;
import javax.swing.UIManager;
import java.io.File;
import driver.ted.utilities.T_JulianDate;
import java.util.prefs.Preferences;


/**
 *
 * @author  tdriver
 */
public class Jed extends javax.swing.JFrame {
    
    JournalArchive archive = new JournalArchive();
    JournalEntry currentEntry = null; //new JournalEntry();
    File currentFile;
        
    Color backColor = Color.white, textColor = Color.black;
    DynamicTree treePanel = new DynamicTree();
    javax.swing.JFileChooser archiveChooser = new javax.swing.JFileChooser(); 
    Preferences prefs = Preferences.userNodeForPackage(this.getClass());
    
    // define the entries for the color values the user chose
    final String backColorPref = "backColor";
    final String frontColorPref = "frontColor";
    
    // specifies if the current entry needs to be saved to the archive;
    boolean saved = false; 
    
    
    /** Creates new form Jed */
    public Jed() {
        setIconImage(java.awt.Toolkit.getDefaultToolkit().createImage(getClass().getResource("images/embarsicon.gif")));
        createActions();
        changeEditingStatus(false);        
        initComponents();
        hidePane = new javax.swing.JPanel();
        
        // get rid of the border on the treepanel
          treePanel.setBorder(null);

          // add the Archive Opener Filter          
          archiveChooser.addChoosableFileFilter(new TextFilter());
          archiveChooser.addChoosableFileFilter(new ArchiveFilter());
          archiveChooser.setFileView(new JedFileView());
 
  
          //Listen for selection changes.
          treePanel.getTree().addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
              public void valueChanged(javax.swing.event.TreeSelectionEvent e) {
                  performGetAction();
              }
          });
          // listen for key presses
          treePanel.getTree().addKeyListener(new java.awt.event.KeyAdapter(){
              public void keyPressed(java.awt.event.KeyEvent e){
                 performKeyAction(e);
              }
        /*      public void keyTyped(java.awt.event.KeyEvent e){
                  performKeyAction(e);
              }
              public void keyReleased(java.awt.event.KeyEvent e){
                  performKeyAction(e);
              } */
          });
          jTextPane1.setVisible(false);
          jTextPane1.setFont(new Font("Courier New",Font.PLAIN,14));
          updateTitles();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
        buttonGroup1 = new javax.swing.ButtonGroup();
        buttonGroup2 = new javax.swing.ButtonGroup();
        jPopupMenu1 = new javax.swing.JPopupMenu();
        jMenuItem4 = new javax.swing.JMenuItem();
        jSeparator9 = new javax.swing.JSeparator();
        ColorPresets = new javax.swing.JMenu();
        gOnBlackButton = new javax.swing.JRadioButtonMenuItem();
        wOnBlueButton = new javax.swing.JRadioButtonMenuItem();
        blackOnWButton = new javax.swing.JRadioButtonMenuItem();
        oOnPurpleButton = new javax.swing.JRadioButtonMenuItem();
        wOnBlackButton = new javax.swing.JRadioButtonMenuItem();
        setBackColorPop = new javax.swing.JMenuItem();
        setTextColorPop = new javax.swing.JMenuItem();
        jSeparator5 = new javax.swing.JSeparator();
        FontPresets = new javax.swing.JMenu();
        CourierNew14 = new javax.swing.JRadioButtonMenuItem();
        TimesNewRoman12 = new javax.swing.JRadioButtonMenuItem();
        SanSerif16 = new javax.swing.JRadioButtonMenuItem();
        Arial14 = new javax.swing.JRadioButtonMenuItem();
        statusBar = new javax.swing.JPanel();
        statusLeft = new javax.swing.JLabel();
        statusRight = new javax.swing.JLabel();
        centerPane = new javax.swing.JPanel();
        jSplitPane1 = new javax.swing.JSplitPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextPane1 = new javax.swing.JTextPane();
        jToolBar1 = new javax.swing.JToolBar();
        openArchiveButton = new javax.swing.JButton();
        saveArchiveButton = new javax.swing.JButton();
        newEntryButton = new javax.swing.JButton();
        closeArchiveButton = new javax.swing.JButton();
        DateStampButton = new javax.swing.JButton();
        helpButton = new javax.swing.JButton();
        jSeparator51 = new javax.swing.JSeparator();
        jToggleButton1 = new javax.swing.JToggleButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        FileMenu = new javax.swing.JMenu();
        openMenuItem = new javax.swing.JMenuItem();
        jSeparator11 = new javax.swing.JSeparator();
        newEntryMenuItem = new javax.swing.JMenuItem();
        jSeparator10 = new javax.swing.JSeparator();
        saveArchiveMenuItem = new javax.swing.JMenuItem();
        saveArchiveAs = new javax.swing.JMenuItem();
        SaveEntryAs = new javax.swing.JMenuItem();
        jSeparator111 = new javax.swing.JSeparator();
        importJMenu = new javax.swing.JMenu();
        directoryImport = new javax.swing.JMenuItem();
        exportJMenu = new javax.swing.JMenu();
        directoryExport = new javax.swing.JMenuItem();
        zipFileExport = new javax.swing.JMenuItem();
        jSeparator4 = new javax.swing.JSeparator();
        closeFileMenuItem = new javax.swing.JMenuItem();
        jSeparator2 = new javax.swing.JSeparator();
        exitFileMenuItem = new javax.swing.JMenuItem();
        Options = new javax.swing.JMenu();
        showHideContents = new javax.swing.JCheckBoxMenuItem();
        showToolBar = new javax.swing.JCheckBoxMenuItem();
        jSeparator1 = new javax.swing.JSeparator();
        ColorPresets1 = new javax.swing.JMenu();
        gOnBlackButton1 = new javax.swing.JRadioButtonMenuItem();
        wOnBlueButton1 = new javax.swing.JRadioButtonMenuItem();
        blackOnWButton1 = new javax.swing.JRadioButtonMenuItem();
        oOnPurpleButton1 = new javax.swing.JRadioButtonMenuItem();
        wOnBlackButton1 = new javax.swing.JRadioButtonMenuItem();
        SetBackColor = new javax.swing.JMenuItem();
        SetTextColor = new javax.swing.JMenuItem();
        jSeparator3 = new javax.swing.JSeparator();
        FontPresets1 = new javax.swing.JMenu();
        CourierNew141 = new javax.swing.JRadioButtonMenuItem();
        TimesNewRoman121 = new javax.swing.JRadioButtonMenuItem();
        SanSerif161 = new javax.swing.JRadioButtonMenuItem();
        Arial141 = new javax.swing.JRadioButtonMenuItem();
        jSeparator6 = new javax.swing.JSeparator();
        dateTimeStampMenuItem = new javax.swing.JMenuItem();
        addPasswordToggle = new javax.swing.JCheckBoxMenuItem();
        Help = new javax.swing.JMenu();
        jedHelp = new javax.swing.JMenuItem();
        About = new javax.swing.JMenuItem();

        jMenuItem4.setAction(dateTimeStampAction);
        jPopupMenu1.add(jMenuItem4);

        jPopupMenu1.add(jSeparator9);

        ColorPresets.setText("Color Presets");
        gOnBlackButton.setBackground(java.awt.Color.black);
        gOnBlackButton.setForeground(java.awt.Color.yellow);
        buttonGroup1.add(gOnBlackButton);
        gOnBlackButton.setAction(goldOnBlackAction);
        ColorPresets.add(gOnBlackButton);

        wOnBlueButton.setBackground(java.awt.Color.blue);
        wOnBlueButton.setForeground(java.awt.Color.white);
        buttonGroup1.add(wOnBlueButton);
        wOnBlueButton.setAction(whiteOnBlueAction);
        ColorPresets.add(wOnBlueButton);

        blackOnWButton.setBackground(java.awt.Color.white);
        blackOnWButton.setSelected(true);
        buttonGroup1.add(blackOnWButton);
        blackOnWButton.setAction(blackOnWhiteAction);
        ColorPresets.add(blackOnWButton);

        oOnPurpleButton.setBackground(new java.awt.Color(102, 0, 102));
        oOnPurpleButton.setForeground(java.awt.Color.orange);
        buttonGroup1.add(oOnPurpleButton);
        oOnPurpleButton.setAction(orangeOnPurpleAction);
        ColorPresets.add(oOnPurpleButton);

        wOnBlackButton.setBackground(java.awt.Color.black);
        wOnBlackButton.setForeground(java.awt.Color.white);
        buttonGroup1.add(wOnBlackButton);
        wOnBlackButton.setAction(whiteOnBlackAction);
        ColorPresets.add(wOnBlackButton);

        jPopupMenu1.add(ColorPresets);

        setBackColorPop.setText("Item");
        setBackColorPop.setAction(setBackgroundColorAction);
        jPopupMenu1.add(setBackColorPop);

        setTextColorPop.setText("Item");
        setTextColorPop.setAction(setTextColorAction);
        jPopupMenu1.add(setTextColorPop);

        jPopupMenu1.add(jSeparator5);

        FontPresets.setText("Font Presets");
        CourierNew14.setFont(new java.awt.Font("Courier New", 0, 14));
        CourierNew14.setSelected(true);
        buttonGroup2.add(CourierNew14);
        CourierNew14.setAction(setCourierFontAction);
        FontPresets.add(CourierNew14);

        TimesNewRoman12.setFont(new java.awt.Font("Times New Roman", 0, 12));
        buttonGroup2.add(TimesNewRoman12);
        TimesNewRoman12.setAction(setTNRFontAction);
        FontPresets.add(TimesNewRoman12);

        SanSerif16.setFont(new java.awt.Font("SansSerif", 1, 16));
        buttonGroup2.add(SanSerif16);
        SanSerif16.setAction(setSanSerifFontAction);
        FontPresets.add(SanSerif16);

        Arial14.setFont(new java.awt.Font("Arial", 0, 14));
        buttonGroup2.add(Arial14);
        Arial14.setAction(setArialFontAction);
        FontPresets.add(Arial14);

        jPopupMenu1.add(FontPresets);

        setTitle("Jed");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                exitForm(evt);
            }
            public void windowDeiconified(java.awt.event.WindowEvent evt) {
                formWindowDeiconified(evt);
            }
        });

        statusBar.setLayout(new java.awt.GridLayout(1, 2));

        statusLeft.setText(" ");
        statusLeft.setBorder(new javax.swing.border.BevelBorder(javax.swing.border.BevelBorder.LOWERED));
        statusBar.add(statusLeft);

        statusRight.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        statusRight.setBorder(new javax.swing.border.BevelBorder(javax.swing.border.BevelBorder.LOWERED));
        statusBar.add(statusRight);

        getContentPane().add(statusBar, java.awt.BorderLayout.SOUTH);

        centerPane.setLayout(new java.awt.GridLayout(1, 0));

        jSplitPane1.setLeftComponent(treePanel);
        jSplitPane1.setDividerLocation(215);
        jSplitPane1.setOneTouchExpandable(true);
        jTextPane1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextPane1KeyPressed(evt);
            }
        });

        jTextPane1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jTextPane1MousePressed(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jTextPane1MouseReleased(evt);
            }
        });

        jScrollPane1.setViewportView(jTextPane1);

        jSplitPane1.setRightComponent(jScrollPane1);

        centerPane.add(jSplitPane1);

        getContentPane().add(centerPane, java.awt.BorderLayout.CENTER);

        jToolBar1.setRollover(true);
        openArchiveButton.setToolTipText("");
        openArchiveButton.setAction(openAction);
        openArchiveButton.setFocusPainted(false);
        openArchiveButton.setMaximumSize(new java.awt.Dimension(25, 25));
        openArchiveButton.setMinimumSize(new java.awt.Dimension(25, 25));
        openArchiveButton.setPreferredSize(new java.awt.Dimension(25, 25));
        openArchiveButton.setText("");
        jToolBar1.add(openArchiveButton);

        saveArchiveButton.setToolTipText("");
        saveArchiveButton.setAction(saveJournalAction);
        saveArchiveButton.setFocusPainted(false);
        saveArchiveButton.setMaximumSize(new java.awt.Dimension(25, 25));
        saveArchiveButton.setMinimumSize(new java.awt.Dimension(25, 25));
        saveArchiveButton.setPreferredSize(new java.awt.Dimension(25, 25));
        saveArchiveButton.setText("");
        jToolBar1.add(saveArchiveButton);

        newEntryButton.setToolTipText("");
        newEntryButton.setAction(newEntryAction);
        newEntryButton.setFocusPainted(false);
        newEntryButton.setMaximumSize(new java.awt.Dimension(25, 25));
        newEntryButton.setMinimumSize(new java.awt.Dimension(25, 25));
        newEntryButton.setPreferredSize(new java.awt.Dimension(25, 25));
        newEntryButton.setText("");
        jToolBar1.add(newEntryButton);

        closeArchiveButton.setToolTipText("");
        closeArchiveButton.setAction(closeAction);
        closeArchiveButton.setMaximumSize(new java.awt.Dimension(25, 25));
        closeArchiveButton.setMinimumSize(new java.awt.Dimension(25, 25));
        closeArchiveButton.setPreferredSize(new java.awt.Dimension(25, 25));
        closeArchiveButton.setText("");
        jToolBar1.add(closeArchiveButton);

        DateStampButton.setToolTipText("");
        DateStampButton.setAction(dateTimeStampAction);
        DateStampButton.setMaximumSize(new java.awt.Dimension(25, 25));
        DateStampButton.setMinimumSize(new java.awt.Dimension(25, 25));
        DateStampButton.setPreferredSize(new java.awt.Dimension(25, 25));
        DateStampButton.setText("");
        jToolBar1.add(DateStampButton);

        helpButton.setToolTipText("");
        helpButton.setAction(jedHelpAction);
        helpButton.setMaximumSize(new java.awt.Dimension(25, 25));
        helpButton.setMinimumSize(new java.awt.Dimension(25, 25));
        helpButton.setPreferredSize(new java.awt.Dimension(25, 25));
        helpButton.setText("");
        jToolBar1.add(helpButton);

        jSeparator51.setOrientation(javax.swing.SwingConstants.VERTICAL);
        jToolBar1.add(jSeparator51);

        jToggleButton1.setToolTipText("Press to show or hide the window contents");
        jToggleButton1.setAction(showHideAction);
        jToggleButton1.setFocusPainted(false);
        jToggleButton1.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        jToggleButton1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jToggleButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton1ActionPerformed(evt);
            }
        });

        jToolBar1.add(jToggleButton1);

        getContentPane().add(jToolBar1, java.awt.BorderLayout.NORTH);

        FileMenu.setMnemonic('F');
        FileMenu.setText("File");
        openMenuItem.setMnemonic('J');
        openMenuItem.setAction(openAction);
        FileMenu.add(openMenuItem);

        FileMenu.add(jSeparator11);

        newEntryMenuItem.setAction(newEntryAction);
        FileMenu.add(newEntryMenuItem);

        FileMenu.add(jSeparator10);

        saveArchiveMenuItem.setAction(saveJournalAction);
        FileMenu.add(saveArchiveMenuItem);

        saveArchiveAs.setAction(saveJournalAsAction);
        FileMenu.add(saveArchiveAs);

        SaveEntryAs.setAction(saveEntryAsAction);
        FileMenu.add(SaveEntryAs);

        FileMenu.add(jSeparator111);

        importJMenu.setText("Import Journal From");
        directoryImport.setAction(importFromDirectoryAction);
        importJMenu.add(directoryImport);

        FileMenu.add(importJMenu);

        exportJMenu.setText("Export Journal To");
        directoryExport.setAction(exportToDirectoryAction);
        exportJMenu.add(directoryExport);

        zipFileExport.setAction(exportToZipFileAction);
        exportJMenu.add(zipFileExport);

        FileMenu.add(exportJMenu);

        FileMenu.add(jSeparator4);

        closeFileMenuItem.setAction(closeAction);
        FileMenu.add(closeFileMenuItem);

        FileMenu.add(jSeparator2);

        exitFileMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_X, java.awt.event.InputEvent.ALT_MASK));
        exitFileMenuItem.setMnemonic('x');
        exitFileMenuItem.setText("Exit");
        exitFileMenuItem.setToolTipText("Exit Jed");
        exitFileMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitFileMenuItemActionPerformed(evt);
            }
        });

        FileMenu.add(exitFileMenuItem);

        jMenuBar1.add(FileMenu);

        Options.setMnemonic('O');
        Options.setText("Options");
        showHideContents.setToolTipText("");
        showHideContents.setAction(showHideAction);
        showHideContents.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                showHideContentsActionPerformed(evt);
            }
        });

        Options.add(showHideContents);

        showToolBar.setSelected(true);
        showToolBar.setAction(showToolbarAction);
        Options.add(showToolBar);

        Options.add(jSeparator1);

        ColorPresets1.setText("Color Presets");
        gOnBlackButton1.setBackground(java.awt.Color.black);
        gOnBlackButton1.setForeground(java.awt.Color.yellow);
        buttonGroup1.add(gOnBlackButton1);
        gOnBlackButton1.setAction(goldOnBlackAction);
        ColorPresets1.add(gOnBlackButton1);

        wOnBlueButton1.setBackground(java.awt.Color.blue);
        wOnBlueButton1.setForeground(java.awt.Color.white);
        buttonGroup1.add(wOnBlueButton1);
        wOnBlueButton1.setAction(whiteOnBlueAction);
        ColorPresets1.add(wOnBlueButton1);

        blackOnWButton1.setBackground(java.awt.Color.white);
        blackOnWButton1.setSelected(true);
        buttonGroup1.add(blackOnWButton1);
        blackOnWButton1.setAction(blackOnWhiteAction);
        ColorPresets1.add(blackOnWButton1);

        oOnPurpleButton1.setBackground(new java.awt.Color(102, 0, 102));
        oOnPurpleButton1.setForeground(java.awt.Color.orange);
        buttonGroup1.add(oOnPurpleButton1);
        oOnPurpleButton1.setAction(orangeOnPurpleAction);
        ColorPresets1.add(oOnPurpleButton1);

        wOnBlackButton1.setBackground(java.awt.Color.black);
        wOnBlackButton1.setForeground(java.awt.Color.white);
        buttonGroup1.add(wOnBlackButton1);
        wOnBlackButton1.setAction(whiteOnBlackAction);
        ColorPresets1.add(wOnBlackButton1);

        Options.add(ColorPresets1);

        SetBackColor.setAction(setBackgroundColorAction);
        Options.add(SetBackColor);

        SetTextColor.setAction(setTextColorAction);
        Options.add(SetTextColor);

        Options.add(jSeparator3);

        FontPresets1.setText("Font Presets");
        CourierNew141.setFont(new java.awt.Font("Courier New", 0, 14));
        CourierNew141.setSelected(true);
        buttonGroup2.add(CourierNew141);
        CourierNew141.setAction(setCourierFontAction);
        FontPresets1.add(CourierNew141);

        TimesNewRoman121.setFont(new java.awt.Font("Times New Roman", 0, 12));
        buttonGroup2.add(TimesNewRoman121);
        TimesNewRoman121.setAction(setTNRFontAction);
        FontPresets1.add(TimesNewRoman121);

        SanSerif161.setFont(new java.awt.Font("SansSerif", 1, 16));
        buttonGroup2.add(SanSerif161);
        SanSerif161.setAction(setSanSerifFontAction);
        FontPresets1.add(SanSerif161);

        Arial141.setFont(new java.awt.Font("Arial", 0, 14));
        buttonGroup2.add(Arial141);
        Arial141.setAction(setArialFontAction);
        FontPresets1.add(Arial141);

        Options.add(FontPresets1);

        Options.add(jSeparator6);

        dateTimeStampMenuItem.setAction(dateTimeStampAction);
        Options.add(dateTimeStampMenuItem);

        addPasswordToggle.setAction(lockJournalAction);
        Options.add(addPasswordToggle);

        jMenuBar1.add(Options);

        Help.setMnemonic('H');
        Help.setText("Help");
        jedHelp.setAction(jedHelpAction);
        Help.add(jedHelp);

        About.setAction(aboutAction);
        Help.add(About);

        jMenuBar1.add(Help);

        setJMenuBar(jMenuBar1);

        pack();
        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setSize(new java.awt.Dimension(655, 521));
        setLocation((screenSize.width-655)/2,(screenSize.height-521)/2);
    }//GEN-END:initComponents

    private void showHideContentsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_showHideContentsActionPerformed
        // Add your handling code here:
    }//GEN-LAST:event_showHideContentsActionPerformed

    private void jToggleButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jToggleButton1ActionPerformed
        Container pane = getContentPane();
        if(jToggleButton1.isSelected()){
            jToggleButton1.setText("Show Contents");
            pane.remove(centerPane);
            pane.add(hidePane, java.awt.BorderLayout.CENTER);
            pane.repaint();
        }else{
            jToggleButton1.setText("Hide Contents");
            pane.remove(hidePane);
            pane.add(centerPane, java.awt.BorderLayout.CENTER);
            pane.repaint();
        }
    }//GEN-LAST:event_jToggleButton1ActionPerformed

    private void jTextPane1KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextPane1KeyPressed
         if(evt.getKeyCode() == evt.VK_F1){
            //System.out.println("F1 Pressed");
            jedHelpAction.actionPerformed(null);
        }
         if(!archive.isDirty()){
            archive.setDirty(true);
            updateTitles();
         }
    }//GEN-LAST:event_jTextPane1KeyPressed

    private void formWindowDeiconified(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowDeiconified
        this.repaint();
    }//GEN-LAST:event_formWindowDeiconified

    private void jTextPane1KeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextPane1KeyTyped
        if(!archive.isDirty()){
            archive.setDirty(true);
            updateTitles();
         }
    }//GEN-LAST:event_jTextPane1KeyTyped

    private void jTextPane1MouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTextPane1MouseReleased
        if (evt.isPopupTrigger()){
            jPopupMenu1.show(jTextPane1,evt.getX(),evt.getY());
        }
    }//GEN-LAST:event_jTextPane1MouseReleased

    private void jTextPane1MousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTextPane1MousePressed
        if (evt.isPopupTrigger()){
            jPopupMenu1.show(jTextPane1,evt.getX(),evt.getY());
        }
    }//GEN-LAST:event_jTextPane1MousePressed

    private void exitFileMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitFileMenuItemActionPerformed
       if(tryClosingArchive(evt))
            System.exit(0);
    }//GEN-LAST:event_exitFileMenuItemActionPerformed
                                    
    /** Exit the Application */
    private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm
        java.awt.event.ActionEvent aevt = null;
        if(tryClosingArchive(aevt))
            System.exit(0);
    }//GEN-LAST:event_exitForm
    
    /**
     *Private function to get a journal entry based upon a click on throws tree or menu command
     */
    private void performGetAction(){
        
        int ns = treePanel.nodeSelected();
        
        if(ns == treePanel.CATEGORY_NODE){
            // do something whn category is selected
            //statusLeft.setText("Category: " + treePanel.getObject());
        }else if(ns == treePanel.LEAF_NODE){
            
            String selectionName = treePanel.getObject();
            JournalEntry retrieved = archive.getJournalEntry(selectionName);
            if (retrieved != null){
                currentEntry = retrieved;
                jTextPane1.setDocument(currentEntry.getDocument());
                if(!jTextPane1.isVisible())
                    jTextPane1.setVisible(true);  
                jTextPane1.setBackground(getStoredBackColor());
                jTextPane1.setForeground(getStoredFrontColor());
                jTextPane1.setCaretColor(getStoredFrontColor());
               // jTextPane1.grabFocus();
                
            }else{
              //statusLeft.setText("No Entry to Retrieve!");
            }
        }else if (ns == treePanel.ROOT_NODE){
            //statusLeft.setText("Archive: " + treePanel.getObject());
        }
        
    }
    
    /** private function to handle key events on the treepanel
     */
    private void performKeyAction(java.awt.event.KeyEvent e){
        if(e.getKeyCode() == e.VK_DELETE){
            // find out what kind of node was selected
            int ns = treePanel.nodeSelected();
            String type;
            if(ns == treePanel.ROOT_NODE || ns == treePanel.UNKNOWN_NODE){
                javax.swing.JOptionPane.showMessageDialog(this,
                "The root node cannot be deleted",
                "Unable to Delete",
                javax.swing.JOptionPane.ERROR_MESSAGE);
                return;
            }
            if( javax.swing.JOptionPane.showConfirmDialog(this,
                "About to delete selection!",
                "Delete Selection",
                javax.swing.JOptionPane.OK_CANCEL_OPTION,
                javax.swing.JOptionPane.WARNING_MESSAGE) == javax.swing.JOptionPane.CANCEL_OPTION){
                    return;
            }
            // if we are here, elected to delete
            // if a leaf node, remove from the archive
            // assume the current entry is the selected entry
             if(ns == treePanel.LEAF_NODE){
                 //remove from the tree
                treePanel.removeCurrentNode();
                archive.removeEntry(currentEntry);
                // removeCurrentNode sets the selected object to the category above, now check if category is empty
                String category = (String)treePanel.getObject();
                //System.out.println("category = " + category);
                if(treePanel.checkForLast()){ // if there are no more entries in this category
                    treePanel.removeCurrentNode();
                    archive.removeJournalType(category);
                    //System.out.println("Removed category!");
                }               
             }else if (ns == treePanel.CATEGORY_NODE){
                 archive.removeJournalType((String)treePanel.getObject()); // removes all entries in this category as well.             
                 //remove from the tree
               // String category = 
                treePanel.removeCurrentNode();
                
             }
            jTextPane1.setVisible(false);
            archive.setDirty(true);
            updateTitles();
        }else  if(e.getKeyCode() == e.VK_F1){
           //System.out.println("F1 Pressed");
            jedHelpAction.actionPerformed(null);
        }
    }
    
    /** Update the Journal Name to indicate the journal
     * saved status
     */    
    private void updateTitles() {
      String caption;

      if (currentFile == null) {
         // synthesize the "Untitled" name if no name yet.
         caption = "Untitled";
      }else{
        caption = currentFile.getName();
      }

      // add a "*" in the caption if the file is dirty.
      if (archive.isDirty()) {
        caption += " *";
      }
      this.setTitle("Jed - " + caption);
    }
    /**Will ask if the archive can be saved if it has been updated, optionally canceling
     */
    public boolean tryClosingArchive(java.awt.event.ActionEvent evt){
        int response = javax.swing.JOptionPane.YES_OPTION;
        if(!archive.okToAbandon()){
            if(evt == null){
                response = javax.swing.JOptionPane.showConfirmDialog(this,
                                   "Save changes to the Journal?",
                                    "Save Journal?",
                                    javax.swing.JOptionPane.YES_NO_OPTION);
            }else{
                response = javax.swing.JOptionPane.showConfirmDialog(this,
                                   "Save changes to the Journal?",
                                    "Save Journal?",
                                    javax.swing.JOptionPane.YES_NO_CANCEL_OPTION);
            }
            if(response == javax.swing.JOptionPane.YES_OPTION){
                    saveJournalAction.actionPerformed(evt);
            }
        }// okToAbandon
        if(response != javax.swing.JOptionPane.CANCEL_OPTION){
             archive = new JournalArchive();
             currentFile = null;
             archive.setArchiveName(treePanel,currentFile);
             currentEntry = null;
             jTextPane1.setVisible(false);
             archive.setDirty(false);
             archive.clear(treePanel);
             updateTitles();
             addPasswordToggle.setSelected(false);
             changeEditingStatus(false);
             return true;
        }
        jTextPane1.grabFocus();
        return false;
    }
    
        
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        try {
      //UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
      //UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
      //UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
      UIManager.setLookAndFeel(new com.incors.plaf.kunststoff.KunststoffLookAndFeel());

    }
    catch(Exception e) {
      e.printStackTrace();
    }
        
    JWindow splash = new JWindow();
    splash.getContentPane().add(new splashPane(),BorderLayout.CENTER);
    splash.setSize(366,108);
    // Center the splash
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension splashSize = splash.getSize();
    if (splashSize.height > screenSize.height) {
      splashSize.height = screenSize.height;
    }
    if (splashSize.width > screenSize.width) {
      splashSize.width = screenSize.width;
    }
    splash.setLocation((screenSize.width - splashSize.width) / 2, (screenSize.height - splashSize.height) / 2);
    splash.setVisible(true);
    // instantiate app
    Jed app = new Jed();
    splash.setVisible(false);
    app.setVisible(true);
    if (args.length >= 1){
        System.out.println("Arg[0] = "+ args[0]);
        app.tryOpeningSuppliedArchive(args[0]);
    }
  }
    
    private void tryOpeningSuppliedArchive(String filename){
        File tempFile = new File(filename);
            // do not want to assign the open file to currentFile if a text file is being opened
            // so only do that if the file extension is Utils.arc...then we've opned an archive.
                              
                    currentFile = tempFile;
                    if(archive.openArchive(currentFile,treePanel )){
                        if(archive.isPasswordEnabled()){
                            addPasswordToggle.setSelected(true);
                        }
                        //treePanel.reload(); // collapses the tree, like ot leave it open
                        archive.setArchiveName(treePanel,currentFile);
                        archive.setDirty(false);
                        updateTitles();
                        changeEditingStatus(true);
                    }else{ // probably did not pass the password check
                        archive = new JournalArchive();
                        currentFile = null;
                        currentEntry = null;
                        jTextPane1.setVisible(false);
                        updateTitles();
                    }                    
//                }else{ // either text or other file, try to open
//                    // check if archive already exists
//                    try{
//                        if(currentFile == null)
//                            newEntryAction.actionPerformed(evt);
//                        if(currentEntry != null){
//                            archive.openFile(tempFile);
//                            archive.setDirty(true);
//                            updateTitles();
//                            changeEditingStatus(true);
//                        }
//                    }catch(java.io.IOException ioe){
//                        javax.swing.JOptionPane.showMessageDialog(Jed.this,"Unknown File Type!",
//                        "Error opening File",
//                        javax.swing.JOptionPane.ERROR_MESSAGE);
//                    }catch(RuntimeException ee){
//                        javax.swing.JOptionPane.showMessageDialog(Jed.this,"Unknown error opening file!\n" + ee.toString(),
//                        "Error opening File",
//                        javax.swing.JOptionPane.ERROR_MESSAGE);
//                    }
               // }
            jTextPane1.grabFocus();
            Jed.this.repaint();
    }
    
    // change enabled status for  editing actions in the program
    private void changeEditingStatus(boolean status){
        saveJournalAction.setEnabled(status);
        saveJournalAsAction.setEnabled(status);
        saveEntryAsAction.setEnabled(status);
        closeAction.setEnabled(status);
        dateTimeStampAction.setEnabled(status);
        goldOnBlackAction.setEnabled(status);
        whiteOnBlackAction.setEnabled(status);
        whiteOnBlueAction.setEnabled(status);
        orangeOnPurpleAction.setEnabled(status);
        blackOnWhiteAction.setEnabled(status);
        setCourierFontAction.setEnabled(status);
        setSanSerifFontAction.setEnabled(status);
        setTNRFontAction.setEnabled(status);
        setArialFontAction.setEnabled(status);
        exportToDirectoryAction.setEnabled(status);
        exportToZipFileAction.setEnabled(status);
        setBackgroundColorAction.setEnabled(status);
        setTextColorAction.setEnabled(status);
        lockJournalAction.setEnabled(status);
        
    }
    
    private void createActions(){
// File Menu        
        // create the open action
        openAction =  new ActionOpen("Open ...",
              new javax.swing.ImageIcon(getClass().getResource("/driver/ted/images/Open16.gif")),
              "Opens journals or text files",new Integer ('J'));
        
        

        // create the new entry action
        newEntryAction =  new ActionNewEntry("New Entry",
              new javax.swing.ImageIcon(getClass().getResource("/driver/ted/images/New16.gif")),
              "Creates a new journal entry",
              new Integer('N'));        
        
        // create the Save Journal action
        saveJournalAction =  new ActionSaveJournal();
        saveJournalAction.setName("Save Journal");
        saveJournalAction.setSmallIcon(new javax.swing.ImageIcon(getClass().getResource("/driver/ted/images/Save16.gif")));
        saveJournalAction.setShortDescription("Saves the current journal");
        saveJournalAction.setMnemonic(new Integer('S'));
             
// create Save Journal As action
        saveJournalAsAction =  new ActionSaveJournalAs("Save Journal As...",
            new javax.swing.ImageIcon(getClass().getResource("/driver/ted/images/SaveAs16.gif")),
            "Saves the journal with a new name",
            new Integer('A')); 
        
// create Save Entry As action
        saveEntryAsAction =  new ActionSaveEntryAs("Save Entry As...",
        new javax.swing.ImageIcon(getClass().getResource("/driver/ted/images/SaveEntryAs.gif")),
            "Saves the journal entry to a text file",
            new Integer('E')); 
        
// create the Import Journal From Directory
        importFromDirectoryAction =  new ActionImportFromDirectory("Directory...",
        new javax.swing.ImageIcon(getClass().getResource("/driver/ted/images/Import16.gif")),
            "Imports a journal from a filesystem directory",
            null);         
        
// create the Export Journal to Directory action
        exportToDirectoryAction =  new ActionExportToDirectory("Directory...",
        new javax.swing.ImageIcon(getClass().getResource("/driver/ted/images/Export16.gif")),
            "Exports a journal to a filesystem directory",
            null);      
        
// create the Export Journal to ZipFile action
        exportToZipFileAction =  new ActionExportToZipFile("Zip File...",
        new javax.swing.ImageIcon(getClass().getResource("/driver/ted/images/Export16.gif")),
          "Exports a journal to a  zip file",
           null);     
        
        // create the close journal action
        closeAction =  new ActionClose("Close Journal",
            new javax.swing.ImageIcon(getClass().getResource("/driver/ted/images/Close16.gif")),
            "Closes the current journal",
            new Integer('C'));    
        
// Options Menu
        
        // create the show/Hide Action
        showHideAction = new ActionShowContents("Hide Contents",null,
            "Shows or Hides the Contents",
            null);
        
        // create the Show toolbar action
        showToolbarAction =  new ActionShowToolbar("Show Toolbar",null,
            "Shows or hides the toolbar",
            null);  
        
// create the Gold on Black color preset action
        goldOnBlackAction =  new ActionGoldOnBlack("Gold On Black",null,null, null);  
        
        // create the White On Blue color preset action
        whiteOnBlueAction =  new ActionWhiteOnBlue("White On Blue",null,null,null);  
        
        // create the Black On White color preset action
        blackOnWhiteAction = new Jed.ActionBlackOnWhite("Black On White",null,null,null);
        
        // create the Orange On Purple colro preset action
        orangeOnPurpleAction = new Jed.ActionOrangeOnPurple("Orange On Purple",null,null,null);
        
        // create the White on Black color preset action
        whiteOnBlackAction = new ActionWhiteOnBlack("White On Black",null,null,null);
        
        // create the Set Background color action
        setBackgroundColorAction = new ActionSetBackgroundColor("Set Background Color...",
            null,"Sets the editor background color",new Integer('B'));
        
        // create the Set Text Color Action
        setTextColorAction = new ActionSetTextColor("Set Text Color...",
            null,"Sets the editor text color",new Integer('T'));
        
        // create the Courier New 14 Action
        setCourierFontAction = new ActionSetCourierFont("Courier New 14", null, null, null);
        
        // Create the Times New Roman 12 action
        setTNRFontAction = new ActionSetTNRFont("Times New Roman 12", null, null, null);
        
        // create the San Serif 16 action
        setSanSerifFontAction = new Jed.ActionSetSanSerifFont("San Serif 16",null,null,null);
        
        // create the Arial 14 action
        setArialFontAction = new Jed.ActionSetArialFont("Arial 14",null,null,null);
        
        // create the Date/Time Stamp Action
        dateTimeStampAction = new Jed.ActionDateTimeStamp("Date/Time Stamp", 
            new javax.swing.ImageIcon(getClass().getResource("/driver/ted/images/date.gif")),
            "Applys a Date/Time Stamp to the current Journal Entry",
            new Integer('D'));
        
        // create the Lock Journal Action
        lockJournalAction = new Jed.ActionLockJournal("Lock Journal",null,
            "Password protects the journal.",new Integer('L'));
        
// About Menu
        
        // create the JED Help Action
        jedHelpAction = new Jed.ActionJEDHelp("Help...", 
        new javax.swing.ImageIcon(getClass().getResource("/driver/ted/images/Help16.gif")),
        "Loads the Jed Help File", new Integer('P'));
        
        // create the About JED Action
        aboutAction = new Jed.ActionAboutJED("About Jed", 
            new javax.swing.ImageIcon(getClass().getResource("/driver/ted/images/embarsicon.gif")),
            null,null);
    }
        
    private void setColorPrefs(java.awt.Color back, java.awt.Color front) {
        prefs.putInt(backColorPref, back.getRGB());
        prefs.putInt(frontColorPref,front.getRGB());
    }  
    
    private java.awt.Color getStoredBackColor(){
        return new java.awt.Color(prefs.getInt(backColorPref,Color.white.getRGB()));
    }
    
    private java.awt.Color getStoredFrontColor(){
        return new java.awt.Color(prefs.getInt(frontColorPref,Color.black.getRGB()));
    }
        
    // -------------- Define the actions in the program -------------------------------------------------------------
    
// File Menu        
    
// create the open action       
    /** open Journal Action class
     */
    private class ActionOpen extends NewAbstractAction {
        public ActionOpen(){
        }    
        public ActionOpen( String name, Icon icon, String description, Integer mnemonic ){
         
            super(name, icon, description, mnemonic);
        }
        
        public void actionPerformed(ActionEvent evt){
          File tempFile;
            // do not want to assign the open file to currentFile if a text file is being opened
            // so only do that if the file extension is Utils.arc...then we've opned an archive.
          if (JFileChooser.APPROVE_OPTION == archiveChooser.showOpenDialog(Jed.this)) {
            tempFile = archiveChooser.getSelectedFile(); 
            if(Utils.getExtension(tempFile).equals(Utils.arc)){
                // open archive file
                if(currentFile!=null){
                    closeAction.actionPerformed(evt);
                }
                currentFile = tempFile;
                if(archive.openArchive(currentFile,treePanel )){
                    if(archive.isPasswordEnabled()){
                        addPasswordToggle.setSelected(true); 
                    }
                    //treePanel.reload(); // collapses the tree, like ot leave it open
                    archive.setArchiveName(treePanel,currentFile);
                    archive.setDirty(false);
                    updateTitles();
                    changeEditingStatus(true);
                }else{ // probably did not pass the password check
                    archive = new JournalArchive();
                    currentFile = null;
                    currentEntry = null;
                    jTextPane1.setVisible(false);
                    updateTitles();
                }


            }else{ // either text or other file, try to open
                // check if archive already exists
                try{
                    if(currentFile == null)
                        newEntryAction.actionPerformed(evt);
                    if(currentEntry != null){
                        archive.openFile(tempFile);
                        archive.setDirty(true);
                        updateTitles();
                        changeEditingStatus(true);
                    }
                }catch(java.io.IOException ioe){
                    javax.swing.JOptionPane.showMessageDialog(Jed.this,"Unknown File Type!",
                                                            "Error opening File",
                                                           javax.swing.JOptionPane.ERROR_MESSAGE); 
                }catch(RuntimeException ee){
                    javax.swing.JOptionPane.showMessageDialog(Jed.this,"Unknown error opening file!\n" + ee.toString(),
                                                            "Error opening File",
                                                           javax.swing.JOptionPane.ERROR_MESSAGE); 
                }
          }
          }
          jTextPane1.grabFocus();
          Jed.this.repaint();
        }    
    }

    
// create the new entry action    
    private class ActionNewEntry extends NewAbstractAction{
       public ActionNewEntry(){
       }        
       public ActionNewEntry( String name, Icon icon, String description, Integer mnemonic ){
          super(name, icon, description, mnemonic);
       }
        
        public void actionPerformed(ActionEvent evt){
            int ns = treePanel.nodeSelected();

           if(ns == treePanel.CATEGORY_NODE){
                // Get Category Name and add Node there
               String category = treePanel.getObject();
               //System.out.println("Category Node: Category = " + category);
               currentEntry = new JournalEntry(category);
               archive.addJournalEntry(treePanel,category, currentEntry, true);
               jTextPane1.setDocument(currentEntry.getDocument());
               jTextPane1.grabFocus();
               //statusRight.setText("This entry added to Category: \"" + category + "\"");
               updateTitles();
               changeEditingStatus(true);
           }else if(ns == treePanel.LEAF_NODE){
               String category = treePanel.getParentObject();
               //System.out.println("Leaf Node: Category = " + category);
               currentEntry = new JournalEntry(category);
               archive.addJournalEntry(treePanel,category, currentEntry, true);
               jTextPane1.setDocument(currentEntry.getDocument());
               jTextPane1.grabFocus();
               //statusRight.setText("This entry added to Category: \"" + category + "\"");         
               updateTitles();
               changeEditingStatus(true);
            }else if (ns == treePanel.ROOT_NODE){
                String category = javax.swing.JOptionPane.showInputDialog(Jed.this,
                    "Enter the Journal category:");
                if (category != null){
                    currentEntry = new JournalEntry(category);
                    archive.addJournalEntry(treePanel,category, currentEntry, true); 
                    jTextPane1.setDocument(currentEntry.getDocument());
                    // initially the text pane is disabled
                    if(!jTextPane1.isVisible()) jTextPane1.setVisible(true);  
                    jTextPane1.setBackground(getStoredBackColor());
                    jTextPane1.setForeground(getStoredFrontColor());
                    jTextPane1.setCaretColor(getStoredFrontColor());
                    jTextPane1.grabFocus();
                    updateTitles();
                    changeEditingStatus(true);
                }else{
                    // nothing...cancelled
                }
                //statusLeft.setText("Journal: " + treePanel.getObject());
            }else{
                // no node selected!
                String category = javax.swing.JOptionPane.showInputDialog(Jed.this,
                    "Enter the Journal category:");
                if (category != null){
                    currentEntry = new JournalEntry(category);
                    archive.addJournalEntry(treePanel,category, currentEntry, true); 
                    jTextPane1.setDocument(currentEntry.getDocument());
                    // initially the text pane is disabled
                   if(!jTextPane1.isVisible()) jTextPane1.setVisible(true);   
                   jTextPane1.setBackground(getStoredBackColor());
                   jTextPane1.setForeground(getStoredFrontColor());
                   jTextPane1.setCaretColor(getStoredFrontColor());
                   jTextPane1.grabFocus();
                   updateTitles();
                   changeEditingStatus(true);
                }else{
                    // nothing...cancelled
                }
            }
            }    
    }
    
// create the Save Journal action
    
    private class ActionSaveJournal extends NewAbstractAction{
       public ActionSaveJournal(){
       }        
       public ActionSaveJournal( String name, Icon icon, String description, Integer mnemonic ){
          super(name, icon, description, mnemonic);
       }
        
        public void actionPerformed(ActionEvent evt){
          archiveChooser.setSelectedFile(currentFile);
          if(currentFile != null){          
              archive.saveArchive(treePanel,currentFile);
              archive.setDirty(false);
          }else{
            if (JFileChooser.APPROVE_OPTION == archiveChooser.showSaveDialog(Jed.this)) {
                currentFile = archiveChooser.getSelectedFile();
                currentFile = Utils.setExtension(currentFile,"jed");
                archive.saveArchive(treePanel,currentFile);
                archive.setArchiveName(treePanel,currentFile);
                archive.setDirty(false);
            }
          }
          updateTitles();
          jTextPane1.grabFocus();
          Jed.this.repaint();
        }
    }
        
 // create the Save Journal As action
    private class ActionSaveJournalAs extends NewAbstractAction{
       public ActionSaveJournalAs(){
       }        
       public ActionSaveJournalAs( String name, Icon icon, String description, Integer mnemonic ){
          super(name, icon, description, mnemonic);
       }
        
        public void actionPerformed(ActionEvent evt){
           archiveChooser.setSelectedFile(currentFile);
           if (JFileChooser.APPROVE_OPTION == archiveChooser.showSaveDialog(Jed.this)) {
            currentFile = archiveChooser.getSelectedFile();
            archive.saveArchive(treePanel,currentFile);
            archive.setArchiveName(treePanel,currentFile);
            archive.setDirty(false);
            //System.out.println("current File = " + currentFile.getPath());
            updateTitles();
          }    
          Jed.this.repaint();
        }
    }
        
// create Save Entry As action
    private class ActionSaveEntryAs extends NewAbstractAction{
       public ActionSaveEntryAs(){
       }        
       public ActionSaveEntryAs( String name, Icon icon, String description, Integer mnemonic ){
          super(name, icon, description, mnemonic);
       }
        
        public void actionPerformed(ActionEvent evt){
            if (archive.saveAsFile(Jed.this, jTextPane1.getEditorKit(),archiveChooser)){
            //statusRight.setText("File saved successfully");
            }else{
                //statusRight.setText("Error Saving File!");
            }
           Jed.this.repaint();
        }
    }
    
// create the Import Journal From Directory
    private class ActionImportFromDirectory extends NewAbstractAction{
       public ActionImportFromDirectory(){
       }        
       public ActionImportFromDirectory( String name, Icon icon, String description, Integer mnemonic ){
          super(name, icon, description, mnemonic);
       }
        
        public void actionPerformed(ActionEvent evt){
            Jed.this.tryClosingArchive(evt);
            File temp = archive.importFromDirectory(Jed.this,jTextPane1.getEditorKit(),treePanel);
            if(temp != null){
                currentFile = temp;
                archive.setArchiveName(treePanel,currentFile);
                archive.setDirty(true);
                changeEditingStatus(true);
                updateTitles();
           }
           Jed.this.repaint();
        }
    }
        
// create the Export Journal to Directory action
     private class ActionExportToDirectory extends NewAbstractAction{
       public ActionExportToDirectory(){
       }        
       public ActionExportToDirectory( String name, Icon icon, String description, Integer mnemonic ){
          super(name, icon, description, mnemonic);
       }
        
        public void actionPerformed(ActionEvent evt){
            archive.export(Jed.this,jTextPane1.getEditorKit(), false);
        }
     }
        
// create the Export Journal to ZipFile action
     private class ActionExportToZipFile extends NewAbstractAction{
       public ActionExportToZipFile(){
       }        
       public ActionExportToZipFile( String name, Icon icon, String description, Integer mnemonic ){
          super(name, icon, description, mnemonic);
       }
        
        public void actionPerformed(ActionEvent evt){
            archive.export(Jed.this,jTextPane1.getEditorKit(), true);
        }
     }
        
     // create the close journal action
    /** Close Journal Action
     */
    private class ActionClose extends NewAbstractAction{
        public ActionClose(){
        } 
        public ActionClose( String name, Icon icon, String description, Integer mnemonic ){
          super(name, icon, description, mnemonic);
        }
        
        public void actionPerformed(ActionEvent evt){
            tryClosingArchive(evt);
        }    
    }       
        
        
// Options Menu
// create the Show toolbar action
     private class ActionShowContents extends NewAbstractAction{
        public ActionShowContents(){
        } 
        public ActionShowContents( String name, Icon icon, String description, Integer mnemonic ){
          super(name, icon, description, mnemonic);
        }
        
        public void actionPerformed(ActionEvent evt){
            Container pane = getContentPane();
            if(evt.getSource() instanceof javax.swing.JToggleButton){
                if(jToggleButton1.isSelected()){
                    showHideContents.setSelected(true);
                    jToggleButton1.setText("Show Contents");
                    pane.remove(centerPane);
                    pane.add(hidePane, java.awt.BorderLayout.CENTER);
                    pane.repaint();
                }else{
                    showHideContents.setSelected(false);
                    jToggleButton1.setText("Hide Contents");
                    pane.remove(hidePane);
                    pane.add(centerPane, java.awt.BorderLayout.CENTER);
                    pane.repaint();
                }
            }else{ // must have been the checkbox that sent the event
                if(showHideContents.isSelected()){
                    jToggleButton1.setSelected(true);
                    jToggleButton1.setText("Show Contents");
                    pane.remove(centerPane);
                    pane.add(hidePane, java.awt.BorderLayout.CENTER);
                    pane.repaint();
                }else{
                    jToggleButton1.setSelected(false);
                    jToggleButton1.setText("Hide Contents");
                    pane.remove(hidePane);
                    pane.add(centerPane, java.awt.BorderLayout.CENTER);
                    pane.repaint();
                }
            }
        }    
    }
    
    private class ActionShowToolbar extends NewAbstractAction{
        public ActionShowToolbar(){
        } 
        public ActionShowToolbar( String name, Icon icon, String description, Integer mnemonic ){
          super(name, icon, description, mnemonic);
        }
        
        public void actionPerformed(ActionEvent evt){
             if (showToolBar.isSelected()){
                jToolBar1.setVisible(true);
            }else{
                jToolBar1.setVisible(false);
            }
        }    
    }
        
// create the Gold on Black color preset action
    private class ActionGoldOnBlack extends NewAbstractAction{
        public ActionGoldOnBlack(){
        } 
        public ActionGoldOnBlack( String name, Icon icon, String description, Integer mnemonic ){
          super(name, icon, description, mnemonic);
        }
        
        public void actionPerformed(ActionEvent evt){
            textColor = Color.yellow;
            backColor = Color.black;
            jTextPane1.setForeground(textColor);
            jTextPane1.setCaretColor(textColor);
            jTextPane1.setBackground(backColor);
            setColorPrefs(backColor, textColor);
        }    
    }
        
// create the White On Blue color preset action
    private class ActionWhiteOnBlue extends NewAbstractAction{
        public ActionWhiteOnBlue(){
        } 
        public ActionWhiteOnBlue( String name, Icon icon, String description, Integer mnemonic ){
          super(name, icon, description, mnemonic);
        }
        
        public void actionPerformed(ActionEvent evt){
            textColor = Color.white;
            backColor = Color.blue;
            jTextPane1.setForeground(textColor);
            jTextPane1.setCaretColor(textColor);
            jTextPane1.setBackground(backColor);
            setColorPrefs(backColor, textColor);
        }    
    }
        
        // create the Black On White color preset action
    private class ActionBlackOnWhite extends NewAbstractAction{
        public ActionBlackOnWhite(){
        } 
        public ActionBlackOnWhite( String name, Icon icon, String description, Integer mnemonic ){
          super(name, icon, description, mnemonic);
        }
        
        public void actionPerformed(ActionEvent evt){
            textColor = Color.black;
            backColor = Color.white;
            jTextPane1.setForeground(textColor);
            jTextPane1.setCaretColor(textColor);
            jTextPane1.setBackground(backColor);
            setColorPrefs(backColor, textColor);
        }    
    }
        
// create the Orange On Purple colro preset action
    private class ActionOrangeOnPurple extends NewAbstractAction{
        public ActionOrangeOnPurple(){
        } 
        public ActionOrangeOnPurple( String name, Icon icon, String description, Integer mnemonic ){
          super(name, icon, description, mnemonic);
        }
        
        public void actionPerformed(ActionEvent evt){
            textColor = Color.orange;
            backColor = new Color(102,0,102);
            jTextPane1.setForeground(textColor);
            jTextPane1.setCaretColor(textColor);
            jTextPane1.setBackground(backColor);
            setColorPrefs(backColor, textColor);
        }    
    }
        
// create the White on Black color preset action
     private class ActionWhiteOnBlack extends NewAbstractAction{
        public ActionWhiteOnBlack(){
        } 
        public ActionWhiteOnBlack( String name, Icon icon, String description, Integer mnemonic ){
          super(name, icon, description, mnemonic);
        }
        
        public void actionPerformed(ActionEvent evt){
            textColor = Color.white;
            backColor = Color.black;
            jTextPane1.setForeground(textColor);
            jTextPane1.setCaretColor(textColor);
            jTextPane1.setBackground(backColor);
            setColorPrefs(backColor, textColor);
        }    
    }
        
// create the Set Background color action
    private class ActionSetBackgroundColor extends NewAbstractAction{
        public ActionSetBackgroundColor(){
        } 
        public ActionSetBackgroundColor( String name, Icon icon, String description, Integer mnemonic ){
          super(name, icon, description, mnemonic);
        }
        
        public void actionPerformed(ActionEvent evt){
            backColor = javax.swing.JColorChooser.showDialog(Jed.this,"Select a Background Color",Color.white);
            jTextPane1.setBackground(backColor);
            setColorPrefs(backColor, getStoredFrontColor());
        }    
    }
        
// create the Set Text Color Action
    private class ActionSetTextColor extends NewAbstractAction{
        public ActionSetTextColor(){
        } 
        public ActionSetTextColor( String name, Icon icon, String description, Integer mnemonic ){
          super(name, icon, description, mnemonic);
        }
        
        public void actionPerformed(ActionEvent evt){
            textColor = javax.swing.JColorChooser.showDialog(Jed.this,"Select a Color",Color.black);
            jTextPane1.setForeground(textColor);
            jTextPane1.setCaretColor(textColor);
            setColorPrefs(getStoredBackColor(), textColor);
        }    
    }
        
// create the Courier New 14 Action
    private class ActionSetCourierFont extends NewAbstractAction{
        public ActionSetCourierFont(){
        } 
        public ActionSetCourierFont( String name, Icon icon, String description, Integer mnemonic ){
          super(name, icon, description, mnemonic);
        }
        
        public void actionPerformed(ActionEvent evt){
            jTextPane1.setFont(new Font("Courier New",Font.PLAIN,14));
        }    
    }
        
// Create the Times New Roman 12 action
    private class ActionSetTNRFont extends NewAbstractAction{
        public ActionSetTNRFont(){
        } 
        public ActionSetTNRFont( String name, Icon icon, String description, Integer mnemonic ){
          super(name, icon, description, mnemonic);
        }
        
        public void actionPerformed(ActionEvent evt){
            jTextPane1.setFont(new Font("Times New Roman",Font.PLAIN,12));
        }    
    }
        
// create the San Serif 16 action
    private class ActionSetSanSerifFont extends NewAbstractAction{
        public ActionSetSanSerifFont(){
        } 
        public ActionSetSanSerifFont( String name, Icon icon, String description, Integer mnemonic ){
          super(name, icon, description, mnemonic);
        }
        
        public void actionPerformed(ActionEvent evt){
            jTextPane1.setFont(new java.awt.Font("SansSerif", Font.BOLD, 16));
        }    
    }
        
// create the Arial 14 action
    private class ActionSetArialFont extends NewAbstractAction{
        public ActionSetArialFont(){
        } 
        public ActionSetArialFont( String name, Icon icon, String description, Integer mnemonic ){
          super(name, icon, description, mnemonic);
        }
        
        public void actionPerformed(ActionEvent evt){
            jTextPane1.setFont(new Font("Arial",Font.PLAIN,14));
        }    
    }
        
// create the Date/Time Stamp Action
    private class ActionDateTimeStamp extends NewAbstractAction{
        public ActionDateTimeStamp(){
        } 
        public ActionDateTimeStamp( String name, Icon icon, String description, Integer mnemonic ){
          super(name, icon, description, mnemonic);
        }
        
        public void actionPerformed(ActionEvent evt){
             if(currentEntry != null){
                try{
                    Document tempdoc = jTextPane1.getDocument();
                    /* previous entry dependent time stamp
                    String jdate = String.valueOf(archive.getCurrentEntry().getJournalDate());
                     **/
                    String jdate = new T_JulianDate().toMediumString();
                    jdate += "\n";
                    tempdoc.insertString(tempdoc.getLength(),jdate,new SimpleAttributeSet());
                    jTextPane1.grabFocus();
                    archive.setDirty(true);
                    updateTitles();
                }catch(BadLocationException ble){
                   // statusRight.setText(" Bad Location Exception!: " +  ble);
                }
            }
        }    
    }
        
        // create the Lock Journal Action
   private class ActionLockJournal extends NewAbstractAction{
        public ActionLockJournal(){
        } 
        public ActionLockJournal( String name, Icon icon, String description, Integer mnemonic ){
          super(name, icon, description, mnemonic);
        }
        
        public void actionPerformed(ActionEvent evt){
            if(addPasswordToggle.isSelected()){
                initialPasswordDialog pd = new initialPasswordDialog(Jed.this, true);
                pd.setTitle("Enter and Confirm your Password");
                pd.setVisible(true);
                if(pd.getPassword() != null){
                    archive.setPasswordEnabled(true);
                    archive.setPassword(pd.getPassword());
                    // force save of journal once password is implemented
                    saveJournalAction.actionPerformed(evt);
                    javax.swing.JOptionPane.showMessageDialog(Jed.this, 
                        "<HTML><body>This journal is now password protected.<br>  Do NOT forget your password!<br> The information contained in this" +
                        " Journal cannot be retrieved without it!",
                        "Password Set for this Journal",
                        javax.swing.JOptionPane.INFORMATION_MESSAGE);
                    updateTitles();
                } else{// otherwise, cancelled
                    addPasswordToggle.setSelected(false);
                }
            }else{
                passwordDialog pd = new passwordDialog(Jed.this, true);
                pd.setTitle("Enter Password to Clear");
                pd.setVisible(true);
                if(pd.getPassword() != null){
                    if(archive.isPasswordGood(pd.getPassword())){
                        archive.clearPassword();
                        // force save of journal once password is cleared
                        saveJournalAction.actionPerformed(evt);
                    }else{
                        javax.swing.JOptionPane.showMessageDialog(Jed.this, 
                        "Password is invalid, please enter a valid password to disable password protection for this Journal.",
                        "Invalid password",                
                        javax.swing.JOptionPane.ERROR_MESSAGE);
                        addPasswordToggle.setSelected(true);                    
                    }
                }else{// otherwise, cancelled
                    addPasswordToggle.setSelected(true);
                }
            }
        }    
    }
        
// About Menu
        
// create the JED Help Action
    private class ActionJEDHelp extends NewAbstractAction{
        public ActionJEDHelp(){
        } 
        public ActionJEDHelp( String name, Icon icon, String description, Integer mnemonic ){
          super(name, icon, description, mnemonic);
        }
        
        public void actionPerformed(ActionEvent evt){
            // show the new JFrame here with the help file loaded
             new JEDHelp();
        }
    }    
        
// create the About JED Action
    private class ActionAboutJED extends NewAbstractAction{
        public ActionAboutJED(){
        } 
        public ActionAboutJED( String name, Icon icon, String description, Integer mnemonic ){
          super(name, icon, description, mnemonic);
        }
        
        public void actionPerformed(ActionEvent evt){
            JournalEditor_AboutBox dlg = new JournalEditor_AboutBox(Jed.this);
            Dimension dlgSize = dlg.getPreferredSize();
            Dimension frmSize = getSize();
            Point loc = getLocation();
            dlg.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x, (frmSize.height - dlgSize.height) / 2 + loc.y);
            dlg.setModal(true);
            dlg.setVisible(true);
        }
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JSeparator jSeparator9;
    private javax.swing.JMenu Options;
    private javax.swing.JButton closeArchiveButton;
    private javax.swing.JMenuItem openMenuItem;
    private javax.swing.JSeparator jSeparator6;
    private javax.swing.JRadioButtonMenuItem wOnBlackButton;
    private javax.swing.JSeparator jSeparator11;
    private javax.swing.JSeparator jSeparator5;
    private javax.swing.JSeparator jSeparator10;
    private javax.swing.JSeparator jSeparator4;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JSeparator jSeparator51;
    private javax.swing.JMenuItem setBackColorPop;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JMenuItem directoryImport;
    private javax.swing.JPopupMenu jPopupMenu1;
    private javax.swing.JRadioButtonMenuItem SanSerif161;
    private javax.swing.JRadioButtonMenuItem wOnBlueButton1;
    private javax.swing.JMenuItem About;
    private javax.swing.JMenuItem exitFileMenuItem;
    private javax.swing.JRadioButtonMenuItem blackOnWButton;
    private javax.swing.JButton openArchiveButton;
    private javax.swing.JMenuItem jedHelp;
    private javax.swing.JMenuItem SaveEntryAs;
    private javax.swing.ButtonGroup buttonGroup2;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JRadioButtonMenuItem TimesNewRoman12;
    private javax.swing.JMenuItem newEntryMenuItem;
    private javax.swing.JMenu Help;
    private javax.swing.JPanel centerPane;
    private javax.swing.JRadioButtonMenuItem oOnPurpleButton;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JRadioButtonMenuItem oOnPurpleButton1;
    private javax.swing.JButton helpButton;
    private javax.swing.JTextPane jTextPane1;
    private javax.swing.JMenuItem zipFileExport;
    private javax.swing.JCheckBoxMenuItem addPasswordToggle;
    private javax.swing.JRadioButtonMenuItem TimesNewRoman121;
    private javax.swing.JMenu FontPresets1;
    private javax.swing.JLabel statusRight;
    private javax.swing.JRadioButtonMenuItem Arial14;
    private javax.swing.JMenuItem closeFileMenuItem;
    private javax.swing.JMenu importJMenu;
    private javax.swing.JPanel statusBar;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JMenuItem setTextColorPop;
    private javax.swing.JRadioButtonMenuItem CourierNew141;
    private javax.swing.JMenuItem saveArchiveMenuItem;
    private javax.swing.JRadioButtonMenuItem CourierNew14;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JMenu ColorPresets;
    private javax.swing.JRadioButtonMenuItem Arial141;
    private javax.swing.JButton DateStampButton;
    private javax.swing.JRadioButtonMenuItem blackOnWButton1;
    private javax.swing.JMenu ColorPresets1;
    private javax.swing.JCheckBoxMenuItem showToolBar;
    private javax.swing.JMenu FileMenu;
    private javax.swing.JMenuItem dateTimeStampMenuItem;
    private javax.swing.JMenuItem SetBackColor;
    private javax.swing.JLabel statusLeft;
    private javax.swing.JCheckBoxMenuItem showHideContents;
    private javax.swing.JMenuItem jMenuItem4;
    private javax.swing.JButton saveArchiveButton;
    private javax.swing.JMenuItem saveArchiveAs;
    private javax.swing.JSeparator jSeparator111;
    private javax.swing.JRadioButtonMenuItem wOnBlackButton1;
    private javax.swing.JMenu exportJMenu;
    private javax.swing.JMenuItem SetTextColor;
    private javax.swing.JRadioButtonMenuItem gOnBlackButton;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JRadioButtonMenuItem gOnBlackButton1;
    private javax.swing.JMenu FontPresets;
    private javax.swing.JButton newEntryButton;
    private javax.swing.JRadioButtonMenuItem wOnBlueButton;
    private javax.swing.JRadioButtonMenuItem SanSerif16;
    private javax.swing.JToggleButton jToggleButton1;
    private javax.swing.JMenuItem directoryExport;
    // End of variables declaration//GEN-END:variables
    // Action variables
    private ActionOpen openJournalAction;
    private ActionAboutJED aboutAction;
    private ActionBlackOnWhite blackOnWhiteAction;
    private ActionClose closeAction;
    private ActionDateTimeStamp dateTimeStampAction;
    private ActionExportToDirectory exportToDirectoryAction;
    private ActionExportToZipFile exportToZipFileAction;
    private ActionGoldOnBlack goldOnBlackAction;
    private ActionImportFromDirectory importFromDirectoryAction;
    private ActionJEDHelp jedHelpAction;
    private ActionLockJournal lockJournalAction;
    private ActionNewEntry newEntryAction;
    private ActionOpen openAction;
    private ActionOrangeOnPurple orangeOnPurpleAction;
    private ActionSaveEntryAs saveEntryAsAction;
    private ActionSaveJournal saveJournalAction;
    private ActionSaveJournalAs saveJournalAsAction;
    private ActionSetArialFont setArialFontAction;
    private ActionSetBackgroundColor setBackgroundColorAction;
    private ActionSetCourierFont setCourierFontAction;
    private ActionSetSanSerifFont setSanSerifFontAction;
    private ActionSetTextColor setTextColorAction;
    private ActionSetTNRFont setTNRFontAction;
    private ActionShowToolbar showToolbarAction;
    private ActionWhiteOnBlack whiteOnBlackAction;
    private ActionWhiteOnBlue whiteOnBlueAction;
    private ActionShowContents showHideAction;
    
    private javax.swing.JPanel hidePane;
    
    
    private class JournalEditor_AboutBox extends JDialog implements ActionListener {
     JPanel aboutPane = new splashPane();
     JButton button1 = new JButton("Close");
      
      public JournalEditor_AboutBox(Frame parent) {
        super(parent);
        enableEvents(AWTEvent.WINDOW_EVENT_MASK);
        try {
          Init();
        }
        catch(Exception e) {
          e.printStackTrace();
        }
      }
      /**Component initialization*/
      private void Init() throws Exception  {
        this.setTitle("About JED - The Journal Editor");
        this.setResizable(false);       
        this.setSize(366,158);
        button1.addActionListener(this);
        this.getContentPane().add(aboutPane, BorderLayout.CENTER);
        this.getContentPane().add(button1, BorderLayout.SOUTH);
    }
      /**Overridden so we can exit when window is closed*/
      protected void processWindowEvent(WindowEvent e) {
        if (e.getID() == WindowEvent.WINDOW_CLOSING) {
          cancel();
        }
        super.processWindowEvent(e);
      }
      /**Close the dialog*/
      void cancel() {
        dispose();
      }
      /**Close the dialog on a button event*/
      public void actionPerformed(ActionEvent e) {
        if (e.getSource() == button1) {
          cancel();
        }
      }      
    }// end of class JournalEditor_AboutBox
    
}
